using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using ProtoTurtle.BitmapDrawing;
public class test : MonoBehaviour
{
    public int w = 60;
    public int h = 60;
    public int x = 30;
    public int y = 30;
    public int Wlength = 2;
    public int Hlength = 2;
    public RawImage rawImage;
    public Color32[] Data;

    // Start is called before the first frame update
    void Start()
    {
        //Changed();
        //return;
        Data = new Color32[w * h];
        for (int i = 0; i < Data.Length; i++)
        {
            Data[i] = Color.white;
        }
        Texture2D texture = new Texture2D(w, h, TextureFormat.RGBA32, false);
        rawImage.texture = texture;
        //for (int i = 0; i < Hlength; i++)
        //{
        //    for (int j = 0; j < Wlength; j++)
        //    {

        //    }
        //}
        //Image_ im = new Image_(Wlength, Hlength);
        //Image_ im = new Image_(Application.streamingAssetsPath+ "/TFCard/back.png");
        Image_ im = new Image_(Application.streamingAssetsPath + "/confirm.png");
        im.width = Wlength;
        im.height = Hlength;
        im.x = x;
        im.y = y;
        im.SetRotate(im.bmp, 2);
        DrawImage(im);
        //Data[0] = Color.black;
        //Data[32] = Color.black;
        //Data[64] = Color.black;
        //Data[128] = Color.black;
        texture.SetPixelData(Data, 0);
        texture.DrawLine(StartPos, EndPos,Color.black);
        texture.Apply();
        rawImage.SetNativeSize();
    }
    public Vector3 StartPos;
    public Vector3 EndPos;
    public int readInt()
    {
        int i = binaryReader.ReadByte();
        int j = binaryReader.ReadByte();
        int k = binaryReader.ReadByte();
        int m = binaryReader.ReadByte();
        if ((i | j | k | m) < 0)
            throw new System.Exception();
        return (i << 24) + (j << 16) + (k << 8) + (m << 0);
    }

    public short readShort()
    {
        int i = binaryReader.ReadByte();
        int j = binaryReader.ReadByte();
        if ((i | j) < 0)
            throw new System.Exception();
        return (short)((i << 8) + (j << 0));
    }
    BinaryReader binaryReader;
    public void Changed()
    {
        FileStream file = File.OpenRead(Application.streamingAssetsPath + "/resources.dat");
        binaryReader = new BinaryReader(file);

        //uint ind = binaryReader.ReadUInt32();
        int ind = readInt();
        binaryReader.ReadBytes(10);
        short si = (short)(readShort() - 1);
        binaryReader.ReadBytes(6);
        short si2 = readShort();
        int[] iArr = new int[si2];
        for (int i = 0; i < si2; i++)
        {
            iArr[i] = readInt();
        }

        short[] c = new short[(si * 2)];
        int[][] d = new int[si][];
        int[] iArr2 = null;
        int[][] iArr3 = null;

        for (int i2 = 0; i2 < si; i2++)
        {
            binaryReader.ReadBytes(6);
            short readShort1 = readShort();
            short readShort2 = readShort();
            c[i2 * 2] = readShort1;
            c[(i2 * 2) + 1] = readShort2;
            int i3 = readShort1 * readShort2;
            if (i3 == 0)
            {
                iArr3 = d;
                iArr2 = null;
            }
            else
            {
                iArr2 = new int[i3];
                for (int i4 = 0; i4 < i3; i4++)
                {
                    int v = binaryReader.ReadByte();
                    iArr2[i4] = iArr[v + 128];
                    if (i2 >= 11 && i2 <= 15 && (iArr2[i4] & -16777216) == -16777216)
                    {
                        iArr2[i4] = iArr2[i4] & -1593835521;
                    }
                }
                iArr3 = d;
            }
            iArr3[i2] = iArr2;

        }
    }

    public int x0 = 17;
    public int y0 = 0;

    public void DrawImage(Image_ image)
    {
        int index = 0;
        int X = image.x - image.width / 2;

        int Y = image.y - image.height / 2;
        for (int i = y0; i < image.height; i++)
        {
            for (int j = x0; j < image.width; j++)
            {
                //index = j +(Width- image.x)/2 + i * image.width + (Height- image.y)/2 * Width;
                int XValue = j + X;
                int YValue = i + Y;
                if (XValue >= 0 && YValue >= 0)
                {
                    index = XValue + (h - 1 - YValue) * w;
                    Data[index] = image.GetColor32(j, i);
                }
                //texture.SetPixel(j+image.x, i+image.y, image.GetColor(j,i));
            }
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}
